﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// MemberInfo扩展
/// </summary>
public static class ESF_MethodExtend
{
    /// <summary>
    /// 是否有ObsoleteAttribute属性
    /// </summary>
    /// <param name="_memberInfo">成员</param>
    /// <returns>true:有,false:无</returns>
    public static bool HasObsoleteAttribute(this MemberInfo _memberInfo)
    {
        return _memberInfo.GetCustomAttribute<ObsoleteAttribute>() != null;
    }

    /// <summary>
    /// 获得方法CSharp访问关键字
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>CSharp关键字</returns>
    public static string GetCSharpAccessKeyword(this MethodInfo _method)
    {
        string keyword = string.Empty;
        if (_method.IsPrivate)
        {
            keyword = enESF_CSharpKeyword.privateKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        else if (_method.IsPublic)
        {
            keyword = enESF_CSharpKeyword.publicKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        else
        {
            keyword = enESF_CSharpKeyword.protectedKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        return keyword;
    }

    /// <summary>
    /// 获得方法MethodAttributes
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>MethodAttributes</returns>
    public static MethodAttributes GetCSharpMethodAttributes(this MethodInfo _method)
    {
        return _method.Attributes;
    }

    /// <summary>
    ///  是否是override
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpOverride(this MethodInfo _method)
    {
        return _method.IsCSharpAbstract() || _method.IsCSharpVirtual();
    }

    /// <summary>
    ///  是否是Abstract
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpAbstract(this MethodInfo _method)
    {
        return _method.IsAbstract;
    }

    /// <summary>
    ///  是否是Sealed
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpSealed(this MethodInfo _method)
    {
        return _method.IsFinal;
    }

    /// <summary>
    ///  是否是Sealed
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpProtected(this MethodInfo _method)
    {
        return !_method.IsPrivate && !_method.IsPublic;
    }

    /// <summary>
    ///  是否Private
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpPrivate(this MethodInfo _method)
    {
        return _method.IsPrivate;
    }

    /// <summary>
    ///  是否Public
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpPublic(this MethodInfo _method)
    {
        return _method.IsPublic;
    }

    /// <summary>
    ///  是否是Virtual
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpVirtual(this MethodInfo _method)
    {
        return _method.IsVirtual;
    }

    /// <summary>
    /// 获得方法CSharp返回参数类型名称
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>CSharp关键字</returns>
    public static string GetCSharpReturnParameterTypeName(this MethodInfo _method)
    {
        return _method.ReturnParameter.ParameterType.GetCSharpTypeCode();
    }

    /// <summary>
    /// 获得递归方法
    /// </summary>
    /// <param name="_type">类型</param>
    /// <param name="_bindFlags">BindingFlags</param>
    /// <param name="_includeInheritInterface">是否包含继承Interface</param>
    /// <returns>属性</returns>
    public static List<MethodInfo> GetRecursionMethods(this Type _type,
        BindingFlags _bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
        bool _includeInheritInterface = false)
    {
        List<MethodInfo> result = new List<MethodInfo>();
        MethodInfo[] rst = _type.GetMethods(_bindFlags);
        if (rst != null && rst.Length > 0)
        {
            result.AddRange(rst);
        }
        if (_type.IsInterface && _includeInheritInterface)
        {
            Type[] types = _type.GetInterfaces();
            if (types != null && types.Length > 0)
            {
                foreach (var t in types)
                {
                    var sub = t.GetRecursionMethods(_bindFlags, _includeInheritInterface);
                    if (sub != null && sub.Count > 0)
                    {
                        result.AddRange(sub);
                    }
                }
            }
        }
        return result;
    }
}
#endif